// 导航栏滚动效果
window.addEventListener('scroll', () => {
  const navbar = document.querySelector('.navbar');
  if (window.scrollY > 400) {
    navbar.classList.add('scrolled');
  } else {
    navbar.classList.remove('scrolled');
  }
});

// 导航栏透明度随滚动渐变
window.addEventListener('scroll', () => {
  const navbar = document.querySelector('.navbar');
  const maxScroll = 900; // 渐变最大滚动距离，拉大渐变区间
  const scrollY = Math.min(window.scrollY, maxScroll);
  const alpha = scrollY / maxScroll;
  // 渐变到深红色
  navbar.style.background = `rgba(139,26,26,${alpha})`;
});

// 移动端菜单切换
const menuToggle = document.getElementById('menuToggle');
const navLinks = document.getElementById('navLinks');

menuToggle.addEventListener('click', () => {
  navLinks.classList.toggle('active');
});

// 动画触发函数
function handleScrollAnimation() {
  const animatedElements = document.querySelectorAll('.animate-element, .animate-left, .animate-right, .animate-scale');
  
  animatedElements.forEach(element => {
    const elementTop = element.getBoundingClientRect().top;
    const windowHeight = window.innerHeight;
    
    // 当元素进入视口时添加动画类
    if (elementTop < windowHeight * 0.8) {
      element.classList.add('animate');
    } else {
      // 当元素离开视口时移除动画类，以便再次触发
      element.classList.remove('animate');
    }
  });
}

// 监听滚动事件
window.addEventListener('scroll', handleScrollAnimation);

// 页面加载时初始检查
document.addEventListener('DOMContentLoaded', () => {
  handleScrollAnimation();
});

// 在窗口大小改变时重新检查动画
window.addEventListener('resize', handleScrollAnimation);

// 平滑滚动
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
  anchor.addEventListener('click', function(e) {
    e.preventDefault();
    const target = document.querySelector(this.getAttribute('href'));
    target.scrollIntoView({
      behavior: 'smooth'
    });
  });
});

// 表单提交处理
const contactForm = document.querySelector('.contact-form form');
if (contactForm) {
  contactForm.addEventListener('submit', (e) => {
    e.preventDefault();
    alert('感谢您的留言！我们会尽快回复。');
    contactForm.reset();
  });
}

// 当前年份
const yearSpan = document.getElementById('currentYear');
if (yearSpan) {
  yearSpan.textContent = new Date().getFullYear();
}
